package com.lck.main.view;

import com.lck.main.game.Chess;
import com.lck.main.model.MyMessage;
import com.lck.main.model.Record;
import com.lck.main.util.SocketUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

public class GameFrame extends JFrame implements ActionListener {
    private GamePanel gp = null;
    private int player;
    private boolean isLocked = false;
    private MyMessage req;

    public void setLocked(boolean locked) {
        this.isLocked = locked;
        gp.setLocked(locked);
    }

    public void setPlayer(int player) {
        this.player = player;
        gp.setCurPlayer(player);
    }

    public GameFrame(){
        req = SocketUtil.getInstance().getReq();
        setTitle(req.getFrom());
        //设置窗口大小
        setSize(550,500);
        //设置点击关闭按钮同时结束虚拟机，每一个java程序都有一个虚拟机
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置布局管理员
        setLayout(new BorderLayout());
        //将游戏面板放到窗口中去
        gp = new GamePanel();
        gp.setGameFrame(this);
        gp.startReceive();
        add(gp,BorderLayout.CENTER);
        //添加按钮面板
        JPanel btnPanel = new JPanel(new GridLayout(4, 1));
        add(btnPanel,BorderLayout.EAST);
        JLabel hintLabel = new JLabel("红方走");
        btnPanel.add(hintLabel);
        gp.setHintLabel(hintLabel);
        JButton btnHuiQi = new JButton("悔棋");
        btnHuiQi.setActionCommand("repent");
        btnHuiQi.addActionListener(this);
        btnPanel.add(btnHuiQi);
        JButton btnSave = new JButton("保存棋谱");
        btnSave.setActionCommand("save");
        btnSave.addActionListener(this);
        btnPanel.add(btnSave);
        JButton btnImp = new JButton("导入棋谱");
        btnImp.setActionCommand("import");
        btnImp.addActionListener(this);
        btnPanel.add(btnImp);
        JButton btnQiuHe = new JButton("求和");
        btnQiuHe.setActionCommand("peace");
        btnQiuHe.addActionListener(this);
        btnPanel.add(btnQiuHe);
        JButton btnSur = new JButton("认输");
        btnSur.setActionCommand("surrender");
        btnSur.addActionListener(this);
        btnPanel.add(btnSur);
        //设置窗口可见,建议放在后面
        setVisible(true);
    }
    public static void main(String[] args) {
//        new GameFrame();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了");
        String command = e.getActionCommand();
        switch (command){
            case "repent":
                System.out.println("repent");
                gp.huiqi();
                break;
            case "save":
                System.out.println("save");
                save();
                break;
            case "import":
                System.out.println("import");
                importC();
                break;
            case "peace":
                qiuhe();
                System.out.println("peace");
                break;
            case "surrender":
                System.out.println("surrender");
                break;
            default:
                break;
        }
    }

    /**
     * 保存棋谱
     */
    private void save(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = chooser.showOpenDialog(null);
        File parent = chooser.getSelectedFile();
        System.out.println("file===>" + parent);
        //创建文件
        String path = parent.getAbsolutePath()+File.separator+System.currentTimeMillis()+".txt";
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            //创建文件输出流对象
            fos = new FileOutputStream(path);
            //对象输出流
            oos = new ObjectOutputStream(fos);
            oos.writeObject(gp.getChesses());
        } catch (Exception es) {
            es.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    oos.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
    }

    /**
     *
     */
    private void qiuhe() {
        int result = JOptionPane.showConfirmDialog(null, "确认向对手求和？", "求和", JOptionPane.YES_NO_OPTION);
        System.out.println(result);
        if (JOptionPane.YES_OPTION == result) {//点击Yes
            //发送求和请求消息
            req.setType(MyMessage.Type.PEACE);
            req.setContent(new Record());
            SocketUtil.getInstance().send(req);
            //锁定棋盘
            gp.setLocked(true);
        }
    }

    /**
     * 导入棋谱
     */
    private void importC(){
        JFileChooser chooser = new JFileChooser();
        int result = chooser.showOpenDialog(null);
        File parent = chooser.getSelectedFile();
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        String path = parent.getAbsolutePath();
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);
            Chess[] chesses = (Chess[]) ois.readObject();
            gp.setChesses(chesses);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
